//
//  PersonViewController.m
//  Movie
//
//  Created by cjy on 16/4/8.
//  Copyright © 2016年 YJC. All rights reserved.
//

#import "PersonViewController.h"
#import <AVOSCloud/AVOSCloud.h>
#import "MeViewController.h"
#import "MeViewController.h"
#import <AVOSCloud/AVOSCloud.h>

@interface PersonViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong)UIButton *returnBtn;
@property (nonatomic,strong)UIImageView *backGroundView;
@property (nonatomic,strong)UIButton *buttontouxing;
@property (nonatomic,strong)UIButton *person;
@property (nonatomic,strong)UILabel *username;
@property (nonatomic,strong)UILabel *userpassword;
@property (nonatomic,strong)UILabel *usermail;

@property (nonatomic,strong)UILabel *nametext;
@property (nonatomic,strong)UILabel *passwordtext;
@property (nonatomic,strong)UILabel *mailtext;



@end

@implementation PersonViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    

    self.backGroundView = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.backGroundView setImage:[UIImage imageNamed:@"1-131030200245Q2"]];
    [self.view addSubview:self.backGroundView];
    
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:2];
        UIVisualEffectView *visualView = [[UIVisualEffectView alloc]initWithFrame:CGRectMake(0, 0 , self.view.frame.size.width, self.view.frame.size.height)];
        visualView.effect = blur;
    
        [self.backGroundView addSubview:visualView];
    
    
    [self createView];
    
    
}

- (void )createView{
    
    _touxiang = [[UIImageView alloc]initWithFrame:CGRectMake(30.f, 50.f, 100.f, 100.f)];
    _touxiang.userInteractionEnabled = YES;
    _touxiang.layer.masksToBounds = YES;
    _touxiang.layer.cornerRadius = 50;
    _touxiang.backgroundColor = [UIColor redColor];
    _touxiang.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 - 60);
    _touxiang.userInteractionEnabled = YES;
    
    [self.view addSubview:_touxiang];
    
    
    NSData *data = [[AVUser currentUser] objectForKey:@"touXiang"];
    if (data != nil) {
        
        _touxiang.image = [UIImage imageWithData:data];
    }else{
        
        _touxiang.image = [UIImage imageNamed:@"1-131030200245Q2"];
        
    }
    _buttontouxing = [[UIButton alloc]initWithFrame:CGRectMake(30.f, 50.f, 100.f, 100.f)];
    _buttontouxing.backgroundColor = [UIColor clearColor];
    _buttontouxing.userInteractionEnabled = YES;
    _buttontouxing.layer.masksToBounds = YES;
    _buttontouxing.layer.cornerRadius = 50;
    _buttontouxing.center = CGPointMake(self.view.center.x, self.view.frame.size.height / 3 - 60);
    [self.view addSubview:_buttontouxing];
    [_buttontouxing addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    
    self.nametext = [[UILabel alloc]initWithFrame:CGRectMake(50,270 , 70, 50)];
    self.nametext.center = CGPointMake(self.backGroundView.center.x - 80, self.backGroundView.center.y - 40);
    self.nametext.text = @"用户名:";
    self.nametext.textColor = [UIColor whiteColor];
    self.nametext.font = [UIFont boldSystemFontOfSize:19];
    [self.backGroundView addSubview:self.nametext];

    self.username = [[UILabel alloc]initWithFrame:CGRectMake(self.backGroundView.center.x - 10, self.backGroundView.center.y - 40 - 25, 200, 50)];
    self.username.textColor = [UIColor whiteColor];
    self.username.font = [UIFont boldSystemFontOfSize:16];
    [self.backGroundView addSubview:self.username];
    
    NSString *currentUsername = [AVUser currentUser].username;// 当前用户名
    if (![AVUser currentUser] ) {
        
        self.username.text = @"暂未登录";
    }else{
        
        self.username.text = currentUsername;
    }
    
//    
//    self.passwordtext = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 100, 50)];
//    self.passwordtext.text = @"密码:";
//    [self.backGroundView addSubview:self.passwordtext];
//    
//    self.userpassword = [[UILabel alloc]initWithFrame:CGRectMake(200, 300, 100, 50)];
//    [self.backGroundView addSubview:self.userpassword];
//    self.userpassword.backgroundColor = [UIColor redColor];
//    NSString *currentPassword = [AVUser currentUser].password;
//    self.userpassword.text = currentPassword;
    
    
    
    
    self.mailtext = [[UILabel alloc]initWithFrame:CGRectMake(50, 300, 70, 50)];
    self.mailtext.center = CGPointMake(self.backGroundView.center.x - 80, self.backGroundView.center.y + 10);
    self.mailtext.text = @"邮    箱:";
    self.mailtext.textColor = [UIColor whiteColor];
    self.mailtext.font = [UIFont boldSystemFontOfSize:19];
    [self.backGroundView addSubview:self.mailtext];
    
    self.usermail = [[UILabel alloc]initWithFrame:CGRectMake(self.backGroundView.center.x - 10, self.backGroundView.center.y + 10 - 25, 200, 50)];
    self.usermail.textColor = [UIColor whiteColor];
    self.usermail.font = [UIFont boldSystemFontOfSize:16];
    [self.backGroundView addSubview:self.usermail];

    NSString *currentEmail = [AVUser currentUser].email; // 当前用户的邮箱
    self.usermail.text = currentEmail;
    if (![AVUser currentUser] ) {
        
        self.usermail.text = @"暂未登录";
    }else{
        
        self.usermail.text = currentEmail;
    }

    
    
    self.returnBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    self.returnBtn.frame = CGRectMake(20, 20, 40, 40);
    [self.returnBtn setTitle:@"返回" forState:UIControlStateNormal];
    self.returnBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [self.returnBtn setTitleColor:[UIColor colorWithRed:100 / 255.f green:160 / 255.f blue:155 / 255.f alpha:1] forState:UIControlStateNormal];
    [self.returnBtn addTarget:self action:@selector(returnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.returnBtn];
}

- (void)add{
    UIImagePickerController *picker = [[UIImagePickerController alloc]init];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:picker.sourceType];
    picker.allowsEditing = YES;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
    // 将image转成Data类型.
}
//上传图片的协议与代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info
{
    //    UIImage *image=[info objectForKey:UIImagePickerControllerEditedImage];
    //    // 将image转成Data类型.
    NSData *dataHeadImage = UIImagePNGRepresentation(info[UIImagePickerControllerEditedImage]);
    
    self.touxiang.image = [UIImage imageWithData:dataHeadImage];
    
    // 将image添加为用户的属性.并保存.
    [[AVUser currentUser] setObject:dataHeadImage forKey:@"touXiang"];
    
    [[AVUser currentUser] saveInBackground];
    //    [self.photo setImage:image forState:UIControlStateNormal];
    //选取完图片之后关闭视图
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)returnAction:(UIButton *)btn{

//    MeViewController *me = [[MeViewController alloc]init];
    [self dismissViewControllerAnimated:YES completion:^{

        [[NSNotificationCenter defaultCenter]postNotificationName:@"dismiss" object:self];
    
    
    }];
//    [self showViewController:me sender:nil];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
